package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.GoodsUnit;
import com.dingreading.cloud.admin.service.GoodsTypeService;
import com.dingreading.cloud.admin.service.GoodsUnitService;
import com.dingreading.cloud.admin.util.bean.GoodsV3Util;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping(value = "/system/goodsUnit")
@Api(tags = "总部-商品单位管理")
public class GoodsUnitController {

    @Resource
    private GoodsUnitService goodsUnitService;
    @Resource
    private GoodsTypeService goodsTypeService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "商品名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<GoodsUnit> pageList = goodsUnitService.pageList(pageUtil, name, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(pageList.getTotalRow());
        data.setList(pageList.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "添加/编辑单位")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "商品id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "商品名") @RequestParam(value = "name") String name
    ) {
        Date time = DateUtil.getNowDatetime();
        if (id == null || id <= 0) {
            // 新增
            GoodsUnit goodsUnit = GoodsV3Util.newGoodsUnit(time, new GoodsUnit(), name, 1);
            goodsUnitService.saveOrUpdate(goodsUnit);
        } else {
            // 修改
            long cnt = goodsUnitService.getSameNameCnt(id, name);
            if (cnt > 0) return R.fail("单位名称已存在");
            // 修改
            GoodsUnit goodsUnit = goodsUnitService.getById(id);
            if (goodsUnit == null) return R.fail("获取失败");

            GoodsV3Util.newGoodsUnit(time, goodsUnit, name, null);
            goodsUnitService.saveOrUpdate(goodsUnit);
        }
        return R.ok();
    }


    @ApiOperation(value = "修改单位启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "启用状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = this.goodsUnitService.updateEnabled(Lists.newArrayList(id), enabled);
        if (!l) return R.fail("修改失败");
        return R.ok();
    }


    @ApiOperation(value = "获取单位详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        GoodsUnit data = goodsUnitService.getById(id);
        if (data == null) return R.fail("获取失败");

        return R.ok(data);
    }

    @ApiOperation(value = "删除单位")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        GoodsUnit data = goodsUnitService.getById(id);
        if (data == null) return R.fail("获取失败");

        boolean b = goodsTypeService.getCntByUnit(data.getName());
        if (b) return R.fail("已被商品规格使用，不能删除");

        goodsUnitService.removeById(id);

        return R.ok();
    }

}
